home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 May: Tool Chest / Developer CD Series Tool Chest (Apple Computer)(May 1999).iso / Tool Chest / Development Kits / MPW etc / MPW-GM / MPW / Examples / SIOWExamples / Count.c next >
Encoding:
C/C++ Source or Header  |  1998-12-03  |  3.8 KB  |  168 lines  |  [TEXT/MPS ]

  1. /*------------------------------------------------------------------------------
  2.  
  3. NAME
  4.       Count -- count lines and characters
  5.  
  6.  
  7. DESCRIPTION
  8.       "Count" counts the lines and characters in its input, and writes the
  9.         counts to standard output.  If more than one file is specified, separate
  10.         counts are written for each file, one per line, preceeded by the file name. 
  11.         A total is also written following the list of files.
  12.         
  13. COPYRIGHT
  14.       Copyright Apple Computer, Inc. 1985-1994
  15.         All rights reserved.
  16.  
  17. ------------------------------------------------------------------------------*/
  18.  
  19. #include <Types.h>
  20. #include <ctype.h>
  21. #include <fcntl.h>
  22. #include <string.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25.  
  26. #define    InputSize    (1024*8)
  27.  
  28. /* Variables local to this file */
  29.  
  30. static    char    inputBuffer[InputSize + 1];
  31. static    long    optionsSpecified;
  32. static    long    lineOption;
  33. static    long    charOption;
  34. static    long     validRequest;
  35. static   long  chunk = 10 * 256;
  36. static    long  chunklet = 256;
  37.  
  38. struct counts {
  39.     long    lines;
  40.     long    characters;
  41. };
  42.     
  43. struct counts count(long input)
  44. {
  45.     char                    c;
  46.     char                    *ptr;
  47.     long                    lines = 0;
  48.     long                    characters = 0;
  49.     long                    charsRead;
  50.     struct    counts    cnts;
  51.     
  52.     while ((charsRead = read(input, inputBuffer, InputSize)) > 0) {
  53.         ptr = inputBuffer;
  54.         inputBuffer[charsRead] = 0;
  55.         characters += charsRead;
  56.         while ((c = *ptr++) != 0 || ptr <= &inputBuffer[charsRead]) {
  57.             if (c == '\n') {
  58.                 lines++;
  59.                 
  60.             }
  61.         }
  62.     }    
  63.     if (characters > 0 && *(ptr-2) != '\n')
  64.         lines++;
  65.    cnts.lines = lines;
  66.     cnts.characters = characters;
  67.     return cnts;
  68. }
  69.  
  70.  
  71. void print(long files, long max, char *name, struct counts cnts)
  72. {
  73.     long    space;
  74.     
  75.     space = 0;
  76.     if (files > 1) {
  77.         fprintf(stdout,"%s ", name);
  78.         space = max - strlen(name);
  79.     }
  80.     if (optionsSpecified == false || lineOption == true) {
  81.         fprintf(stdout, "%*d ", space + 5, cnts.lines);
  82.         space = 0;
  83.     }
  84.     if (optionsSpecified == false || charOption == true) {
  85.         fprintf(stdout, "%*d ", space + 7, cnts.characters);
  86.     }
  87.     fprintf(stdout, "\n");
  88.     fflush(stdout);
  89.     /* modify fprintf(stdout, to printf( and take out fflush() to get it to work */
  90. }
  91.  
  92. #define MAX 100        
  93.  
  94. main()
  95. {
  96.    char                     *lptr;
  97.     char                    request[20];
  98.     char                    *fileList[MAX];
  99.     long                    lcount = 0;
  100.     long                    listincr;
  101.     long                    files = 0;
  102.     long                    fcnt;
  103.     long                    done;
  104.     long                    input;
  105.     long                    max;
  106.     struct    counts    cnts;
  107.     struct    counts    total;
  108.  
  109.     max = strlen("Total");
  110.     optionsSpecified = lineOption = charOption = validRequest = false;    
  111.     printf("Report desired (C[haracters], L[ines], B[oth]:");
  112.     lptr = request;
  113.     do {
  114.         gets(lptr);  
  115.         if (*request == 'c' || *request == 'C') {
  116.             optionsSpecified = charOption = true;
  117.             validRequest = true;
  118.         } else if (*request == 'l' || *request == 'L') {
  119.             optionsSpecified = lineOption = true;
  120.             validRequest = true;
  121.         } else if (*request == 'b' || *request == 'B' || *request == 0) 
  122.             validRequest = true;
  123.         else 
  124.             printf("try again: ");
  125.     } while (validRequest == false);
  126.     
  127.     printf("Filenames (enter one per line (minimum: one); end with a blank line\n");    
  128.         for (;;) {
  129.             if (files++ % 10 == 0) { 
  130.                 lptr = (char *) malloc(chunk);
  131.                 fileList[lcount++] = lptr;
  132.             }
  133.         printf("> ");
  134.         if ( gets(lptr), *lptr == 0 || files == MAX ) break;
  135.         lptr += chunklet;
  136.         }
  137.     files--;
  138.     printf("\n");
  139.     
  140.     total.lines = total.characters = done = 0;
  141.     for (lcount = 0, fcnt  = 0; lcount <= (files - 1) / 10; lcount++) 
  142.     {
  143.         for (listincr = 0; listincr < chunk; listincr += chunklet, fcnt++)
  144.         {
  145.             char *f;
  146.             if (fcnt >= files)
  147.                 break;
  148.             f = fileList[lcount] + listincr;
  149.             input = open(f, O_RDONLY);
  150.             if (input >= 0) {
  151.                 cnts = count(input);
  152.                 close(input);
  153.                 total.lines += cnts.lines;
  154.                 total.characters += cnts.characters;
  155.                 print(files, max, f, cnts);
  156.                 done++;
  157.             } else {
  158.                 fprintf(stderr,"### - Unable to open file %s.\n", f);
  159.             }
  160.         }
  161.     }
  162.     if (done > 1) {
  163.         print(files, max, "Total", total);
  164.     }
  165.     
  166.     return 0;
  167. }
  168.